package com.example.understand_compose1.testcase_compose.page

import androidx.compose.foundation.background
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.layout.layout
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

fun Modifier.firstBaseline(firstBaseLineToTop : Dp) = this.then(
    layout { measurable, constraints ->
        // 测量元素
        val placeable = measurable.measure(constraints)
        val firstBaseLine = placeable[FirstBaseline]

        val placeableY = firstBaseLineToTop.roundToPx() - firstBaseLine
        val height = placeable.height + placeableY
        layout(placeable.width, height) {
            placeable.place(0, placeableY)
        }
    }
)

// 自定义布局
@Preview
@Composable
fun TextWithPaddingToBaseLine() {
    Text(
        text = "Hello world",
        Modifier.firstBaseline(24.dp)
            .background(Color.Red)
    )
}